using System;
using UnityEngine;

[Serializable]
public class GuaResult
{
    public BaGua ti;
    public BaGua yong;
    public WuXing shangWX;
    public WuXing xiaWX;
    public string guaName;
    public ShengKe shengke;
    public JiXiong jixiong ;
}
[Serializable]
public class MeihuayiResult
{
    public GuaResult zhuGua;
    public GuaResult bianGua;
}
public class Meihuayi
{
    public static MeihuayiResult ZhanBu(int num1, int num2, int num3)
    {
        BaGua shangGua = Meihuayi.GetBaGua(num1);
        BaGua xiaGua = Meihuayi.GetBaGua(num2);  
        
        //Debug.LogError($"num1:{num1} shang Gua:{shangGua}  num2:{num2} xia Gua:{xiaGua}");

        int dongYao = num3 % 6;
        dongYao = dongYao == 0 ? 6 : dongYao;
        
        MeihuayiResult result = new MeihuayiResult();
            
        result.zhuGua = GetGuaResult(shangGua,xiaGua,dongYao);

        BaGua bianGua;
        if (dongYao <= 3)
        {
            //上卦为体,下卦变卦
            int dongYaoIndex = dongYao - 1; 
            bianGua = Meihuayi.GetBianGua(xiaGua, dongYaoIndex);
            
            result.bianGua = GetGuaResult(shangGua,bianGua,dongYao);
        }
        else
        {
            //下卦为体,上卦变卦
            int dongyaoIndex = dongYao - 4;
            bianGua = Meihuayi.GetBianGua(shangGua,dongyaoIndex);
            
            result.bianGua = GetGuaResult(bianGua,xiaGua,dongYao);
        }
        return result;
    }

    public static GuaResult GetGuaResult(BaGua shangBaGua,BaGua xiaBaGua,int dongYao)
    {
        GuaResult guaResult = new GuaResult();
            
        if (dongYao <= 3)
        {
            //上卦为体
            guaResult.ti = shangBaGua;
            guaResult.yong = xiaBaGua;
        }
        else
        {
            //下卦为体
            guaResult.ti = xiaBaGua;
            guaResult.yong = shangBaGua;
        }
        
        //Debug.Log($"shang gua:{shangBaGua}  xia gua:{xiaBaGua}");
        
        guaResult.shangWX = Data.BaGua2WuXing[shangBaGua];
        guaResult.xiaWX = Data.BaGua2WuXing[xiaBaGua];
        
        guaResult.guaName = Data.BaGua2ZiRan[shangBaGua] + Data.BaGua2ZiRan[xiaBaGua]+  GetGuaName(xiaBaGua,shangBaGua) + "卦";
        
        guaResult.shengke = Data.GetShengKe(Data.BaGua2WuXing[guaResult.ti],Data.BaGua2WuXing[guaResult.yong]);
        
        guaResult.jixiong = Data.shengKe2JiXiong[guaResult.shengke];
        
        return guaResult;
    }
    
    /// <summary>
    /// 根据动爻获得变卦
    /// </summary>
    /// <param name="baGua"></param>
    /// <param name="dongYaoIndex"></param>
    /// <returns></returns>
    public static BaGua GetBianGua(BaGua baGua, int dongYaoIndex)
    {
        if (baGua == BaGua.乾)
        {
            if (dongYaoIndex == 0) return BaGua.巽;
            if (dongYaoIndex == 1) return BaGua.离;
            if (dongYaoIndex == 2) return BaGua.兑;
        }
        else if (baGua == BaGua.兑)
        {
            if (dongYaoIndex == 0) return BaGua.坎;
            if (dongYaoIndex == 1) return BaGua.震;
            if (dongYaoIndex == 2) return BaGua.乾;
        }
        else  if (baGua == BaGua.离)
        {
            if (dongYaoIndex == 0) return BaGua.艮;
            if (dongYaoIndex == 1) return BaGua.乾;
            if (dongYaoIndex == 2) return BaGua.震;
        }
        else if (baGua == BaGua.震)
        {
            if (dongYaoIndex == 0) return BaGua.坤;
            if (dongYaoIndex == 1) return BaGua.兑;
            if (dongYaoIndex == 2) return BaGua.离;
        } 
        else if (baGua == BaGua.巽)
        {
            if (dongYaoIndex == 0) return BaGua.乾;
            if (dongYaoIndex == 1) return BaGua.艮;
            if (dongYaoIndex == 2) return BaGua.坎;
        } 
        else if (baGua == BaGua.坎)
        {
            if (dongYaoIndex == 0) return BaGua.兑;
            if (dongYaoIndex == 1) return BaGua.坤;
            if (dongYaoIndex == 2) return BaGua.巽;
        } 
        else if (baGua == BaGua.艮)
        {
            if (dongYaoIndex == 0) return BaGua.离;
            if (dongYaoIndex == 1) return BaGua.巽;
            if (dongYaoIndex == 2) return BaGua.坤;
        }
        else if (baGua == BaGua.坤)
        {
            if (dongYaoIndex == 0) return BaGua.震;
            if (dongYaoIndex == 1) return BaGua.坎;
            if (dongYaoIndex == 2) return BaGua.艮;
        }

        Debug.LogError($"GetBianGua invali, baGua:{baGua} dongYaoIndex:{dongYaoIndex}");
        return BaGua.乾;
    }

    public static BaGua GetBaGua(int num)
    {
        int gua = num % 8;
        gua = gua == 0 ? 8 : gua;
        return (BaGua) gua;
    }

    public static Gua64 GetGuaName(BaGua xiagua, BaGua shangGua)
    {
        if (xiagua == BaGua.乾 && shangGua == BaGua.乾) return Gua64.乾;
        if (xiagua == BaGua.坤 && shangGua == BaGua.坤) return Gua64.坤;
        if (xiagua == BaGua.震 && shangGua == BaGua.坎) return Gua64.屯;
        if (xiagua == BaGua.坎 && shangGua == BaGua.艮) return Gua64.蒙;
        if (xiagua == BaGua.乾 && shangGua == BaGua.坎) return Gua64.需;
        if (xiagua == BaGua.坎 && shangGua == BaGua.乾) return Gua64.讼;
        if (xiagua == BaGua.坎 && shangGua == BaGua.坤) return Gua64.师;
        if (xiagua == BaGua.坤 && shangGua == BaGua.坎) return Gua64.比;
        if (xiagua == BaGua.乾 && shangGua == BaGua.巽) return Gua64.小畜;
        if (xiagua == BaGua.兑 && shangGua == BaGua.乾) return Gua64.履;
        if (xiagua == BaGua.乾 && shangGua == BaGua.坤) return Gua64.泰;
        if (xiagua == BaGua.坤 && shangGua == BaGua.乾) return Gua64.否;
        if (xiagua == BaGua.离 && shangGua == BaGua.乾) return Gua64.同人;
        if (xiagua == BaGua.乾 && shangGua == BaGua.离) return Gua64.大有;
        if (xiagua == BaGua.艮 && shangGua == BaGua.坤) return Gua64.谦;
        if (xiagua == BaGua.坤 && shangGua == BaGua.震) return Gua64.豫;
        if (xiagua == BaGua.震 && shangGua == BaGua.兑) return Gua64.随;
        if (xiagua == BaGua.巽 && shangGua == BaGua.艮) return Gua64.蛊;
        if (xiagua == BaGua.兑 && shangGua == BaGua.坤) return Gua64.临;
        if (xiagua == BaGua.坤 && shangGua == BaGua.巽) return Gua64.观;
        if (xiagua == BaGua.震 && shangGua == BaGua.离) return Gua64.噬嗑;
        if (xiagua == BaGua.离 && shangGua == BaGua.艮) return Gua64.贲;
        if (xiagua == BaGua.坤 && shangGua == BaGua.艮) return Gua64.剥;
        if (xiagua == BaGua.震 && shangGua == BaGua.坤) return Gua64.复;
        if (xiagua == BaGua.震 && shangGua == BaGua.乾) return Gua64.无妄;
        if (xiagua == BaGua.乾 && shangGua == BaGua.艮) return Gua64.大畜;
        if (xiagua == BaGua.震 && shangGua == BaGua.艮) return Gua64.颐;
        if (xiagua == BaGua.巽 && shangGua == BaGua.兑) return Gua64.大过;
        if (xiagua == BaGua.坎 && shangGua == BaGua.坎) return Gua64.坎;
        if (xiagua == BaGua.离 && shangGua == BaGua.离) return Gua64.离;
        if (xiagua == BaGua.艮 && shangGua == BaGua.兑) return Gua64.咸;
        if (xiagua == BaGua.巽 && shangGua == BaGua.震) return Gua64.恒;
        if (xiagua == BaGua.艮 && shangGua == BaGua.乾) return Gua64.遁;
        if (xiagua == BaGua.乾 && shangGua == BaGua.震) return Gua64.大壮;
        if (xiagua == BaGua.坤 && shangGua == BaGua.离) return Gua64.晋;
        if (xiagua == BaGua.离 && shangGua == BaGua.坤) return Gua64.明夷;
        if (xiagua == BaGua.离 && shangGua == BaGua.巽) return Gua64.家人;
        if (xiagua == BaGua.兑 && shangGua == BaGua.离) return Gua64.睽;
        if (xiagua == BaGua.艮 && shangGua == BaGua.坎) return Gua64.蹇;
        if (xiagua == BaGua.坎 && shangGua == BaGua.震) return Gua64.解;
        if (xiagua == BaGua.兑 && shangGua == BaGua.艮) return Gua64.损;
        if (xiagua == BaGua.震 && shangGua == BaGua.巽) return Gua64.益;
        if (xiagua == BaGua.乾 && shangGua == BaGua.兑) return Gua64.夬;
        if (xiagua == BaGua.巽 && shangGua == BaGua.乾) return Gua64.姤;
        if (xiagua == BaGua.坤 && shangGua == BaGua.兑) return Gua64.萃;
        if (xiagua == BaGua.巽 && shangGua == BaGua.坤) return Gua64.升;
        if (xiagua == BaGua.坎 && shangGua == BaGua.兑) return Gua64.困;
        if (xiagua == BaGua.巽 && shangGua == BaGua.坎) return Gua64.井;
        if (xiagua == BaGua.离 && shangGua == BaGua.兑) return Gua64.革;
        if (xiagua == BaGua.巽 && shangGua == BaGua.离) return Gua64.鼎;
        if (xiagua == BaGua.震 && shangGua == BaGua.震) return Gua64.震;
        if (xiagua == BaGua.艮 && shangGua == BaGua.艮) return Gua64.艮;
        if (xiagua == BaGua.艮 && shangGua == BaGua.巽) return Gua64.渐;
        if (xiagua == BaGua.离 && shangGua == BaGua.震) return Gua64.丰;
        if (xiagua == BaGua.艮 && shangGua == BaGua.离) return Gua64.旅;
        if (xiagua == BaGua.巽 && shangGua == BaGua.巽) return Gua64.巽;
        if (xiagua == BaGua.兑 && shangGua == BaGua.兑) return Gua64.兑;
        if (xiagua == BaGua.坎 && shangGua == BaGua.巽) return Gua64.涣;
        if (xiagua == BaGua.兑 && shangGua == BaGua.坎) return Gua64.节;
        if (xiagua == BaGua.兑 && shangGua == BaGua.巽) return Gua64.中孚;
        if (xiagua == BaGua.艮 && shangGua == BaGua.震) return Gua64.小过;
        if (xiagua == BaGua.离 && shangGua == BaGua.坎) return Gua64.既济;
        if (xiagua == BaGua.坎 && shangGua == BaGua.离) return Gua64.未济;
        
        return Gua64.乾;
    }
}